﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_DBUtility;
using System.Data.SqlClient;
using System.Data;
using V5_Model;

using System.Data.OleDb;
namespace V5_DAL.SqlServer
{
    public class DALFriend
    {

        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_Friend");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Friend");
            strSql.Append(" where ID=" + ID);
            return DbHelperSQL.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelFriend model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null)
            {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.FriendTitle != null)
            {
                strSql1.Append("FriendTitle,");
                strSql2.Append("'" + model.FriendTitle + "',");
            }
            if (model.FriendUrl != null)
            {
                strSql1.Append("FriendUrl,");
                strSql2.Append("'" + model.FriendUrl + "',");
            }
            if (model.FriendLogo != null)
            {
                strSql1.Append("FriendLogo,");
                strSql2.Append("'" + model.FriendLogo + "',");
            }
            if (model.FriendReadme != null)
            {
                strSql1.Append("FriendReadme,");
                strSql2.Append("'" + model.FriendReadme + "',");
            }
            if (model.IsShow != null)
            {
                strSql1.Append("IsShow,");
                strSql2.Append("" + model.IsShow + ",");
            }
            strSql.Append("insert into K_Friend(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelFriend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Friend set ");
            if (model.FriendTitle != null)
            {
                strSql.Append("FriendTitle='" + model.FriendTitle + "',");
            }
            if (model.FriendUrl != null)
            {
                strSql.Append("FriendUrl='" + model.FriendUrl + "',");
            }
            if (model.FriendLogo != null)
            {
                strSql.Append("FriendLogo='" + model.FriendLogo + "',");
            }
            if (model.FriendReadme != null)
            {
                strSql.Append("FriendReadme='" + model.FriendReadme + "',");
            }
            if (model.IsShow != null)
            {
                strSql.Append("IsShow=" + model.IsShow + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Friend ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelFriend GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from K_Friend ");
            strSql.Append(" where ID=" + ID);
            ModelFriend model = new ModelFriend();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FriendTitle"] != null && ds.Tables[0].Rows[0]["FriendTitle"].ToString() != "")
                {
                    model.FriendTitle = ds.Tables[0].Rows[0]["FriendTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FriendUrl"] != null && ds.Tables[0].Rows[0]["FriendUrl"].ToString() != "")
                {
                    model.FriendUrl = ds.Tables[0].Rows[0]["FriendUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FriendLogo"] != null && ds.Tables[0].Rows[0]["FriendLogo"].ToString() != "")
                {
                    model.FriendLogo = ds.Tables[0].Rows[0]["FriendLogo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FriendReadme"] != null && ds.Tables[0].Rows[0]["FriendReadme"].ToString() != "")
                {
                    model.FriendReadme = ds.Tables[0].Rows[0]["FriendReadme"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(ds.Tables[0].Rows[0]["IsShow"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method

        #region K_Friend开始
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Friend ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount)
        {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public List<ModelFriend> GetModelList(string strWhere)
        {
            DataSet ds = this.GetList(strWhere);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return DBCommonHelper.FillModel<ModelFriend>(ds);
            }
            return null;
        }

        public List<ModelFriend> GetModelList(int Top, string strWhere, string filedOrder)
        {
            DataSet ds = this.GetList(Top, strWhere, filedOrder);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return DBCommonHelper.FillModel<ModelFriend>(ds);
            }
            return null;
        }

        public List<ModelFriend> GetModelList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount)
        {
            DataSet ds = this.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return DBCommonHelper.FillModel<ModelFriend>(ds);
            }
            return null;
        }

        public DataSet GetList(string strFiled, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            if (filedOrder.Trim() != "")
                strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount)
        {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperSQL.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1)
            {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from K_Friend where 1=1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1)
            {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From K_Friend Where 1=1 ");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder))
                {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder))
            {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion K_Friend结束




    }
}
